﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace NetworkPong
{
    class Paddle
    {
        public const float speed = 10.0f;

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        private Vector2 position;

        private Vector2 origin;

        private Texture2D texture;

        public Rectangle BB
        {
            get
            {
                return new Rectangle
                (
                    (int) (position.X - origin.X),
                    (int) (position.Y - origin.Y),
                    (int) (texture.Width),
                    (int) (texture.Height)
                );
            }
        }

        private int screenHeight;

        public Paddle(int gamerIndex, ContentManager content, int screenWidth, int screenHeight)
        {
            this.texture = content.Load<Texture2D>("Paddle");
            this.origin = new Vector2(texture.Width / 2, texture.Height / 2);
            this.screenHeight = screenHeight;

            if (gamerIndex == 0)
            {
                position = new Vector2(20, screenHeight / 2);
            }
            else
            {
                position = new Vector2(screenWidth - 20, screenHeight / 2);
            }
        }

        public void Down()
        {
            position.Y += speed;
        }

        public void Up()
        {
            position.Y -= speed;
        }

        public void Update()
        {
            position.Y = MathHelper.Clamp(position.Y, origin.Y, screenHeight - origin.Y);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, null, Color.Red, 0.0f, origin, 1.0f, SpriteEffects.None, 1.0f);
        }
    }
}
